/*
 * Killocan 2013
 * http://killocan.blogspot.com
*/

#pragma once

#include <string>
#include <vector>
#include <map>

#include "camera.h"
#include "player.h"
#include "animsequence.h"

class Stage
{
  public:
  	Stage(std::string stage_path, Camera & camera, Player ** player);//, std::vector<Character *> & characters_vec);
    ~Stage();

    void createEnemies(std::vector<Character *> & characters_vec);
    Character * createCharacter(unsigned int TYPE, int x, int y, int vx = 0, int vy = 0, void * param = NULL);

  	unsigned char tileAction(int x, int y) const;
    unsigned char tileActionUnnormalized(int x, int y) const;
    unsigned char tileNumberUnnormalized(int x, int y) const;

  	void draw(BITMAP * bmp, const Camera & camera, bool hasFg, bool bg_only=true, bool bg=true);
    bool scrollForbid(Camera & camera, int camerax, int & forbid_dir);
    void doCamera(Player & player, Camera & camera);
    bool cameraSectorHasFgTiles(Camera & camera, Player & player);

    AnimSequence * getAnimSeq(unsigned int TYPE);
    BITMAP * getTileSet();

    void setOffset(int, int);
    int getOffset(int);

    bool has_fg_tiles;
    int max_x, max_y;

    bool horz_scroll;

    Player * m_player;

    bool hasMovingPlatform;
    std::vector<int> platformTiles;

  private:
    struct MAP_INFO
    {
      unsigned char tile_number;
      unsigned char action;
      int xOffset;
      bool isForeground;
    };
    MAP_INFO ** map;

    struct sector_t
    {
      bool scroll_forbid;
      bool has_fg_tiles;
    };
    sector_t * sectors;

    struct waypoint_t
    {
      int x,y;
    };
    std::vector<waypoint_t> waypoints;

    struct tiles_t
    {
      //std::vector<RLE_SPRITE*> tile_img_rle;
      std::vector<BITMAP*> tile_img;
      std::vector<int> x_pos;
    };
    tiles_t tiles;

    BITMAP * tileset;

    int default_tile;

    int cur_waypoint;

    std::map<unsigned int, AnimSequence *> preLoadedSprites;

    std::map<int, int> offsetMap;

    //inline void tileDraw(BITMAP * bmp, int tile_number, int x, int y);
    void loadTiles(const std::string & stage_file);
    int load(const std::string & stage_path, Camera & camera, Player ** player); //, std::vector<Character *> & characters_vec);
    int unload();
};
